package top.mccsdl.Acbot.eventListener.listeners;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.mccsdl.Acbot.BotApplication;
import top.mccsdl.Acbot.config.BotConfig;
import top.mccsdl.Acbot.config.EventName;
import top.mccsdl.Acbot.entity.KeyValue;
import top.mccsdl.Acbot.entity.MediaResource;
import top.mccsdl.Acbot.entity.Message;
import top.mccsdl.Acbot.entity.user.UserInfo;
import top.mccsdl.Acbot.enums.MediaType;
import top.mccsdl.Acbot.eventListener.EventListener;
import top.mccsdl.Acbot.service.MenuService;
import top.mccsdl.Acbot.service.UserService;

import java.util.List;

import static top.mccsdl.Acbot.config.BotConfig.*;

/**
 * 群聊At事件
 */
@EventName("GROUP_AT_MESSAGE_CREATE")
@Slf4j
@Service
public class GroupAtMessageCreateListener implements EventListener {

    @Autowired
    private BotConfig botConfig;
    @Autowired
    private MenuService menuService;
    @Autowired
    private UserService userService;

    @Override
    public void onEvent(JSONObject eventData) {
//        log.info("群聊JSON打印-> {}",eventData.toJSONString());
        String groupId = eventData.getString("group_id");
        String groupOpenId = eventData.getString("group_openid");//群openId
        String msgId = eventData.getString("id");//消息ID
        //发送的消息 content ->  测试
        String content = eventData.getString("content");
        JSONObject userInfo = eventData.getJSONObject("author");
        String union_openid = userInfo.getString("union_openid");//用户唯一ID
        String[] msg = content.trim().split("\\s+");
        Message message = new Message();
        message.setMsgId(msgId);//被动消息
        UserInfo user = userService.groupGetUserInfo(union_openid, groupId, message);
        log.info(DateTime.now() + "群聊:[{}]({})->{}", user.getName(), union_openid, content);//用户艾特调用日志
        switch (msg[0]) {
            case "领取", "/领取" ->
                message.setContent("福利领取成功~");
            case "菜单", "/菜单" ->
                menuService.getMenuMsg(msg, message);
            case "绑定", "/绑定" -> {
            }
            case "查看", "/查看" -> {

            }
            case "初始化", "/初始化" -> {
            }
            case "商城", "/商城" -> {
                MediaResource mediaResource = new MediaResource();
                mediaResource.setFileType(MediaType.IMAGE.getValue());//图片
                mediaResource.setUrl(MCCSDL + "/show");
//                mediaResource.setSrvSendMsg(true);
                //发送图片
//                log.info("群聊At消息文件发送入参===>" + mediaResource.toJsonStr());
                String result = HttpRequest.post(String.format(DOMAIN + POST_GROUP_FILE, groupOpenId))
                        .body(mediaResource.toJsonStr())
                        .header("Authorization", "QQBot " + botConfig.getAccessToken())
                        .execute().body();
                log.info("群聊At消息文件发送返回===>" + result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                message.setMsgType(7);//富媒体
                message.setMedia(jsonObject);
                message.setContent(" ");
            }
            default ->
                message.setContent("哎呀,未知命令捏");
        }
        String result = HttpRequest.post(String.format(DOMAIN + POST_GROUP_MESSAGE, groupOpenId))
                .body(message.toJsonStr())
                .header("Authorization", "QQBot " + botConfig.getAccessToken())
                .execute().body();
        log.info("群聊At消息事件发送返回===>" + result);
    }
}
